package web;

import com.MD5;
import com.DBCon;
import com.DBtools;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String usernum = request.getParameter("usernum");
		String pwd = request.getParameter("pwd");
		String password = MD5.md5(pwd);
		String sql = "SELECT * FROM user WHERE UserNum='" + usernum + "'";
		ResultSet rs = null;
		try {
			rs = new DBCon().find(sql);
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			response.sendRedirect("Login.jsp?b1=0");

		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//输出查询结果
		System.out.println("rs" + rs);
		String UserRole = "";
		String UserNum = "";
		String UserPwd = "";
		try {
			while (rs.next()) {
				UserRole = rs.getString("UserRole");
				UserNum = rs.getString("UserNum");
				UserPwd = rs.getString("UserPwd");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		System.out.println("UserNum" + UserNum);

		if (UserNum == "") {
			//查找不到账号
			System.out.println("账号不存在！！！");
			response.sendRedirect("Login.jsp?b1=0");
		}

		else if (UserPwd.equals(password)) {
			System.out.println("密码正确！！！");
			request.getSession().setAttribute("UserNum", usernum);
			request.getSession().setAttribute("UserPwd", pwd);
			request.getSession().setAttribute("isok", "ok");
					
			// 在教师和学院表里查找身份信息
			ResultSet rs2 = null;
			try {
				rs2 = new DBCon().find("select * from department,teacher where DepNum='" + UserNum + "' or TeaId='" + UserNum + "'");
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			String DepName = "";
			String TeaName = "";
			try {
				while (rs2.next()) {
					DepName = rs2.getString("DepName");
					TeaName = rs2.getString("TeaName");

				}
				rs2.close();

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// 判断身份选择首页，并写入seesion
			if (UserRole.equals("教师")) {
				// 判断进入哪个身份
				request.getSession().setAttribute("UserName", TeaName);
				response.sendRedirect("teacherclock/teachhome.jsp");
			} else if (UserRole.equals("学院联系人")) {
				request.getSession().setAttribute("UserName", DepName);
				response.sendRedirect("depcontacts/teaindex.jsp");
			} else if (UserRole.equals("管理员")) {
				request.getSession().setAttribute("UserName", "管理员");
				response.sendRedirect("Manager/Manager.jsp");
			}
			
		} else {
			System.out.println("密码错误！！！");
			response.sendRedirect("Login.jsp?b2=0");
		}
	}
}
			
       
        
    

